package leihua.crm.workbench.web.controller;

import leihua.crm.commons.contants.Contants;
import leihua.crm.commons.domain.ReturnObject;
import leihua.crm.commons.utils.DateUtils;
import leihua.crm.commons.utils.UUIDUtils;
import leihua.crm.settings.domain.User;
import leihua.crm.workbench.domain.ActivityRemark;
import leihua.crm.workbench.service.ActivityRemarkService;
import leihua.crm.workbench.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 对应  workbench/activity/detail.jsp页面
 * @Author leiHua
 * @Date 2022/10/17 11:52
 */
@Controller
public class ActivityRemarkController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityRemarkService activityRemarkService;

    @RequestMapping("/workbench/activity/saveCreateActivityRemark.do")
    @ResponseBody
    public Object saveCreateActivityRemark(ActivityRemark remark, HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        try {
            String id = UUIDUtils.getUUID();
            remark.setId(id);
            remark.setCreateTime(DateUtils.formateDateTime(new Date()));
            remark.setEditFlag(Contants.REMARK_DEIT_FLAG_NO_EDITED);
            User user = (User)session.getAttribute(Contants.SESSION_USER);
            remark.setCreateBy(user.getId());
            int i=activityRemarkService.saveCreateActivityRemark(remark);
            if (i>0){
                returnObject.setRetData(remark);
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("添加成功");
            }else{
                returnObject.setMessage("添加失败，请联系管理员");
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            }

        } catch (Exception e) {
            returnObject.setMessage("添加失败，请联系管理员");
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/activity/deleteActivityRemarkById.do")
    @ResponseBody
    public Object deleteActivityRemarkById(String id){
        ReturnObject returnObject = new ReturnObject();
        try {
            int i = activityRemarkService.deleteActivityRemarkById(id);
            if (i>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("删除成功");
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("删除失败，请联系管理员");
            }
        } catch (Exception e) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("删除失败，请联系管理员");
            e.printStackTrace();
        }
        return returnObject;
    }
    @RequestMapping("/workbench/activity/saveEditActivityRemark.do")
    @ResponseBody
    public Object saveEditActivityRemark(ActivityRemark remark,HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        //封装参数
        try {
            User user=(User)session.getAttribute(Contants.SESSION_USER);
            remark.setEditBy(user.getId());
            remark.setEditTime(DateUtils.formateDateTime(new Date()));
            remark.setEditFlag(Contants.REMARK_DEIT_FLAG_YES_EDITED);
            int i = activityRemarkService.saveEditActivityRemark(remark);
            if (i>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("修改成功");
                returnObject.setRetData(remark);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("修改失败");
            }
        } catch (Exception e) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("修改失败");
            e.printStackTrace();
        }
        return returnObject;
    }
}
